//
//  LMHomePageViewController.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 liman. All rights reserved.
//

#import "LMHomePageViewController.h"
#import "CK_MenuViewController.h"
#import "LMCollectionViewCell.h"
#import "LMHomeDetailTableViewController.h"
#import "LMProductsController.h"
#import "LMStationViewController.h"
#import "LMPostsController.h"
#import "LMSetController.h"

#import "LMHomeModel.h"

#import "LMAVManager.h"
#import "LMNetWorkRequestManager.h"
#import "LMCycleView.h"
#import "LMCollectController.h"
#import "UIImage+ImageEffects.h"

#import "UserInfoManager.h"



@interface LMHomePageViewController ()<MenuClickDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

///collectionView
@property (nonatomic,strong) UICollectionView *collectionView;
///数据源数组属性
@property (nonatomic, strong) NSMutableArray *dataSource;
///轮播图属性
@property (nonatomic, strong) LMCycleView *cycleView;
@property (nonatomic, strong)CK_MenuViewController *menuVC;
@end

@implementation LMHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    self.navigationItem.title = @"今日❤︎TODAY";
    
    [self setupLeftControllerView];
    [self loadData];
    [self setupCollectiomView];
    
}

#pragma mark - 解析数据
- (void)loadData{
    [LMNetWorkRequestManager requestURL:[NSURL URLWithString:READLIST_URL ] Method:LM_POST Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
//                NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
//                NSLog(@"%@",response);
//                NSLog(@"%@",error);
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDict = jsonDict[@"data"];
            for (NSDictionary *item in dataDict[@"list"]) {
                LMHomeModel *model = [[LMHomeModel alloc]init];
                [model setValuesForKeysWithDictionary:item];
                [self.dataSource addObject:model];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
            
        });
        
    }];
}

#pragma mark - 创建collectionView
- (void)setupCollectiomView{
    
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    //    设置flowLayout
    flowLayout.minimumLineSpacing = 1;
    flowLayout.minimumInteritemSpacing = 1;
    flowLayout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width * 0.33, self.view.frame.size.height * 0.25);
    //    设置Header的高度
    flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 200);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    UIImage *image = [UIImage imageNamed:@"ba.jpg"];
    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[image applyDarkEffect]];
    //    注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"LMCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    //    注册头
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [self.view addSubview:self.collectionView];
}

#pragma mark - UICollectiomViewDataSource

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        //创建头部轮播图
        self.cycleView = [[LMCycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) interVal:3];
        self.cycleView.imgNameArray = [@[@"44.jpg",@"55.jpg",@"66.jpg",@"77.jpg",@"88.jpg"] mutableCopy];
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [header addSubview:self.cycleView];
        return header;
    }
    return nil;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LMCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ///下一页详情
    LMHomeDetailTableViewController *DetailVC = [[LMHomeDetailTableViewController alloc]init];
    LMHomeModel *model = self.dataSource[indexPath.row];
    DetailVC.typeID = model.type;
    DetailVC.model = model;
    [self.navigationController pushViewController:DetailVC animated:YES];
}



#pragma mark - 创建左侧视图
- (void)setupLeftControllerView{
    CK_MenuViewController *menuVC =   [CK_MenuViewController getMenuViewController];
    menuVC.menuDelegate = self;
    menuVC.menuArray = [@[@"今日❤︎TODAY",@"电台❤︎RADIO",@"话题❤︎TOPICS",@"良品❤︎PRODUCTS"] mutableCopy];
    menuVC.menuArray2 = [@[@"收藏❤︎COLLECT",@"设置❤︎SETTING"] mutableCopy];

}

#pragma mark - 点击抽屉按钮

- (IBAction)didClickLeftBarBtnItem:(UIBarButtonItem *)sender {
 
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        [[CK_MenuViewController getMenuViewController] hideLeftViewController];
    }else{
        [[CK_MenuViewController getMenuViewController] showLeftViewController];
    }
}


#pragma mark - 点击菜单代理

-(void)didClickMenuIndex:(NSInteger)index Title:(NSString *)title{
    
    [self.navigationController popToRootViewControllerAnimated:NO];
    switch (index) {

            case 0:
        {
            if ([title isEqualToString:@"收藏❤︎COLLECT"]){
                if ([UserInfoManager shareManager].auth) {
                    LMCollectController *conVC = [[LMCollectController alloc]init];
                    [self.navigationController pushViewController:conVC animated:YES];
                }
                else
                {
                    NSLog(@"没有登录");
                    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有登录，马上去登陆" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        UIStoryboard *storyboad = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                        UIViewController *loginVC = [storyboad instantiateViewControllerWithIdentifier:@"Login"];
                        [self presentViewController:loginVC animated:YES completion:nil];
                    }];
                    [alertC addAction:yesAction];
                    
                    UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleDefault handler:nil];
                    [alertC addAction:noAction];
                    
                    [self presentViewController:alertC animated:YES completion:nil];
                }
                
            }

        }
            break;
        case 1:
        {
            if ([title isEqualToString:@"电台❤︎RADIO"]) {
                LMStationViewController *stationVC = [[LMStationViewController alloc]init];
                stationVC.navigationItem.title = title;
                [self.navigationController pushViewController:stationVC animated:YES];
            }
            if ([title isEqualToString:@"设置❤︎SETTING"]) {
                LMSetController *setVC = [[LMSetController alloc]init];
                setVC.navigationItem.title  =title;
                [self.navigationController pushViewController:setVC animated:YES];
            }
            
            
        }
            break;
        case 2:
        {
            if ([title isEqualToString:@"话题❤︎TOPICS"]) {
                LMPostsController *postsVC = [[LMPostsController alloc]init];
                postsVC.navigationItem.title = title;
                [self.navigationController pushViewController:postsVC animated:YES];
            }
         
            
        }
            break;
        case 3:
        {
            LMProductsController *LMProductC = [[LMProductsController alloc]init];
            LMProductC.navigationItem.title = title;
            [self.navigationController pushViewController:LMProductC animated:YES];
        }
            break;

            
        default:
            break;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
